#include "Game.h"
#include <strsafe.h>
// Game states
#include "Pause.h"
#include "Play.h"

Game::Game(HINSTANCE hInstance, int nShowCmd)
  : Core(hInstance, nShowCmd),
  stateMachine(this)
{
	// Initialise managers
	EntityManager::init();
	MessageHandler::init();
	AnimationPlayer::AnimationPlayer();
	ForceRegistry::init();
	// Add game states
	stateMachine.addState("Pause", new Pause());
	stateMachine.addState("Play", new Play());
	stateMachine.changeState("Play");

	// CONSTRUCT STUFF...
}

Game::~Game()
{
}

void Game::initialise()
{
	// INITIALISE STUFF...
}

void Game::loadContent()
{
	// LOAD STUFF...

	// Create player animation pointer
	AnimationPlayer* playerAnims = new AnimationPlayer();
	// Load player idle texture
	D3DXCreateTextureFromFileEx(device, L"CursorIdle.png",
		D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
		0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
		D3DX_DEFAULT, D3DX_DEFAULT,
		0, 0, 0, &playerIdleTexture);
	// Create animation from texture & set attributes
	Animation* playerIdle = new Animation(playerIdleTexture);
	// Add amination to anims
	playerAnims->addAnimation("Idle", playerIdle);

	// Load run texture
	D3DXCreateTextureFromFileEx(device, L"CursorRotation.png",
		D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
		0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
		D3DX_DEFAULT, D3DX_DEFAULT,
		0, 0, 0, &playerRunTexture);
	// Create animation from texture & set attributes
	Animation* playerRun = new Animation(playerRunTexture);
	// Add amination to anims
	playerAnims->addAnimation("Run", playerRun);

	// Load player texture
	D3DXCreateTextureFromFileEx(device, L"CursorTargeting.png",
		D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
		0, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT,
		D3DX_DEFAULT, D3DX_DEFAULT,
		0, 0, 0, &playerTargetingTexture);
	// Create animation from texture & set attributes
	Animation* playerTarget = new Animation(playerTargetingTexture);
	// Add anims to anims
	playerAnims->addAnimation("Target", playerTarget); 

	// Construct player object
	player = new Player(playerAnims, D3DXVECTOR3(50,50,0));
	

}

void Game::unloadContent()
{
	// UNLOAD STUFF...
	playerRunTexture->Release();
	playerRunTexture = 0;
	
	playerIdleTexture->Release();
	playerIdleTexture = 0;

	playerTargetingTexture->Release();
	playerTargetingTexture = 0;
}

void Game::update(float frameTime)
{
	// AUDIO TEST // When Space Bar is pressed play Zap sound.
	if (inputHandler->isKeyDown(DIK_Z))
		audioState.pSoundBank->Play( audioState.iZap, 0, 0, NULL );

	// When Esc is enter pause screen if it is pressed a second time exit the game.
	if (inputHandler->isKeyDown(DIK_ESCAPE) && !inputHandler->wasKeyDown(DIK_ESCAPE) && stateMachine.getCurrentState() == stateMachine.getState("Play"))
		stateMachine.changeState("Pause");
	else if (inputHandler->isKeyDown(DIK_ESCAPE) && !inputHandler->wasKeyDown(DIK_ESCAPE) && stateMachine.getCurrentState() == stateMachine.getState("Pause"))
		PostQuitMessage(0);
	
	// Update StateMachine and hence managers, dependant on game state.
	stateMachine.update(frameTime);

	// Check for input.
	inputHandler->poll();
	// Make sounds.
	audioState.pEngine->DoWork();
}

void Game::render()
{	
	// Turn on ambient lighting.
//	device->SetRenderState(D3DRS_AMBIENT, 0xffffffff);

	// Clear buffers ready to render next frame.
	device->Clear(0, NULL, D3DCLEAR_TARGET,
				  D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
	
	device->BeginScene();
	sprite->Begin(D3DXSPRITE_ALPHABLEND);
//------Begin scene-------------------------------------------------------------

	// DRAW STUFF...
	stateMachine.draw(sprite);

//------End scene---------------------------------------------------------------
	sprite->End();
	device->EndScene();
	// Draw to back buffer & screen.
	device->Present(0, 0, 0, 0);
}